package com.sina.shop.manage.controller.admin;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.admin.Permission;
import com.sina.shop.manage.service.admin.PermissionManageService;
import com.sina.shop.util.StringUtils;
import com.sina.shop.web.BaseController;
import org.apache.shiro.SecurityUtils;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.security.auth.Subject;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-12-20-16:31
 */
@Controller
@RequestMapping("permissionManage")
public class PermissionManageController extends BaseController {

    @Autowired
    @Qualifier("permissionManageService")
    private PermissionManageService permissionManageService;

    @RequestMapping(value = "getPermissionList", method = RequestMethod.GET)
    public ModelAndView getPermissionList(@Valid Permission permission) {
        LOG.info("permission:{}", permission);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);
        Long count[] = new Long[1];
        List<Map<String, Object>> permissionList = permissionManageService.getPermissionList(permission, count);

        Map<String, Object> data = new HashMap<>(4);
        data.put("recordsTotal", count[0]);
        data.put("recordsFiltered", count[0]);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getPermissionIdAndNameList", method = RequestMethod.GET)
    public ModelAndView getPermissionIdAndNameList(Boolean hasAll) {

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(6);

        List<Map<String, Object>> permissionList = permissionManageService.getPermissionIdAndNameList(hasAll);

        Map<String, Object> data = new HashMap<>(4);
        data.put("data", permissionList);

        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getPermission", method = RequestMethod.GET)
    public ModelAndView getPermission(String name) {
        LOG.info("name:{}", name);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        Permission permission = permissionManageService.getPermissionByName(name);
        result.put("data", permission);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "deletePermission", method = RequestMethod.POST)
    public ModelAndView deletePermission(Long id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            permissionManageService.deletePermission(id);
            result.put(CODE, OK);
            result.put(MSG, "删除权限成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "updatePermission", method = RequestMethod.POST)
    public ModelAndView updatePermission(@Valid Permission permission) {

        LOG.info("permission:{}", permission);

        //设置修改时间
        permission.setUpdateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            permissionManageService.updatePermission(permission);
            result.put(CODE, OK);
            result.put(MSG, "更新权限成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "savePermission", method = RequestMethod.POST)
    public ModelAndView savePermission(@Valid Permission permission, String pids) {

        LOG.info("permission:{}", permission);

        /*创建时间、修改时间、创建人*/
        permission.setCreateTime(LocalDateTime.now());
        permission.setUpdateTime(LocalDateTime.now());
        String createName = (String) SecurityUtils.getSubject().getSession().getAttribute("username");
        permission.setCreateName(createName);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            permissionManageService.savePermission(permission);
            result.put(CODE, OK);
            result.put(MSG, "新增权限成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);

        return mv;
    }


    @RequestMapping(value = "getPermissionListToHtml", method = RequestMethod.GET)
    private ModelAndView getPermissionListToHtml() {

        ModelAndView mv = new ModelAndView("admin/permissionList");

        return mv;
    }
}
